//
//  NCChatMoreInputView.m
//  Masonry
//
//  Created by LINAICAI on 2018/4/9.
//

#import "NCChatMoreInputView.h"
#import "NCChatMoreCell.h"
#import "NCChatMoreCollectionView.h"
static NSString const *cellID = @"NCChatMoreCell";
@interface NCChatMoreInputView()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic , strong)NSArray<NSString *> *titles;
@property (nonatomic , strong)NSArray<UIImage *> *images;

@property (nonatomic , strong)NCChatMoreCollectionView *collectionView;
@property (nonatomic , strong)UICollectionViewFlowLayout *layout;
@end
@implementation NCChatMoreInputView
- (UICollectionViewFlowLayout *)layout{
    if (!_layout) {
        _layout = [[UICollectionViewFlowLayout alloc]init];
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        
        _layout.itemSize = CGSizeMake(87, 87);
        _layout.minimumLineSpacing = 0;
        _layout.minimumInteritemSpacing = 0;
        _layout.sectionInset = UIEdgeInsetsMake(0, 27/2.0, 0, 0);

    }
    return _layout;
}
- (NCChatMoreCollectionView *)collectionView{
    if (!_collectionView) {
        _collectionView = [[NCChatMoreCollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:self.layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor = [UIColor colorWithRed:248/255.0 green:248/255.0 blue:248/255.0 alpha:1.0];
        [_collectionView registerClass:NCChatMoreCell.class forCellWithReuseIdentifier:cellID];
    }
    return _collectionView;
}
- (instancetype)initWithTitle:(NSArray<NSString *> *)titles images:(NSArray<UIImage *> *)images
{
    self = [super init];
    if (self) {
        self.titles = titles;
        self.images = images;
        
        NSAssert(titles.count == images.count, @"more action menu titles'number no equal to images'number!");
        
        [self initialize];
    }
    return self;
}

- (void)initialize{
    [self addSubview:self.collectionView];
    [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            make.left.top.right.equalTo(self);
            make.bottom.equalTo(self.mas_safeAreaLayoutGuideBottom);
        } else {
            make.edges.equalTo(self);
        }
  
    }];

}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.titles.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    NCChatMoreCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.menuImage.image = self.images[indexPath.row];
    cell.menuLabel.text = self.titles[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if (self.delegate && [self.delegate respondsToSelector:@selector(NCChatMoreInputViewDidClickAt:)]) {
        [self.delegate NCChatMoreInputViewDidClickAt:indexPath];
    }
}


@end
